<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*, myclasses.*, java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Provide degrees - Choose location</title>
</head>
<body>
<center>
Please choose the location of your university:
</center>

<%	/*In case of more than one degree*/
	session.setAttribute("currDegree", null);
	session.setAttribute("state", null);
	session.setAttribute("currDegree", null);
	
	Student stud = (Student)session.getAttribute("student");
	
	/*creating address object if US citizen or*/
	if (stud.getAddress() == null) {
		if (request.getParameter("state") != null) {
			Address a = new Address(request.getParameter("street"), request.getParameter("city"), request.getParameter("state"),
					request.getParameter("zip"), request.getParameter("area"), request.getParameter("number"), null);
			stud.setAddress(a);
		} else {
			Address a = new Address(request.getParameter("street"), request.getParameter("city"), null,
					request.getParameter("zip"), request.getParameter("area"), request.getParameter("number"), request.getParameter("countryCode"));
			stud.setAddress(a);
		}
	}
	
	//////////////
	
	
	try {
		Class.forName("org.postgresql.Driver");
	} catch(Exception e) {
		out.println("org.postgresql.Driver Exception. ");
	}

	try {
		String serverName = "localhost:5432";
	    String mydatabase = "ThirdBase";
	    String url = "jdbc:postgresql://" + serverName + "/" + mydatabase; 
	    String username = "SweetAss";
	    String password = "1234";
	    Connection conn = DriverManager.getConnection(url, username, password);
	    
		Statement stmt = conn.createStatement();
		ResultSet rset = stmt.executeQuery("SELECT id,name FROM location");

		Integer columncounter = 0;
		out.println("<br>");
		out.println("<table width=\"600\" border=\"2\" cellpadding=\"2\" cellspacing=\"2\" align=\"center\">");
		
		while(rset.next()){

			columncounter += 1;
			if(columncounter == 1)
				out.println("<tr>");
			
			out.println("<td>" + "<a href=\"http://localhost:8080/GradSchoolApp/provideDegreesChooseUniversities.jsp?stateIndex=" + rset.getString(1) + "\">" + rset.getString(2) + "</a>" + "</td>");
			
			if (columncounter >= 3) {
				out.println("</tr>");
				columncounter = 0;
			}
		}
		out.println("</table>");
		out.println("<br>");
		
		rset.close();
		stmt.close();
    	conn.close();
    
	} catch(SQLException e) {
		e.printStackTrace();
		out.println("Connection exception.");
	}
	
	/////////////
%>
</body>
</html>